package eerbaar.v2.pattern.client.internal.readers;

import eerbaar.game.IGame;
import eerbaar.memory.IMemory;
import eerbaar.message.lib.CollisionMsg;
import eerbaar.persona.IPersona;
import eerbaar.shared.message.IMessageReader;
import eerbaar.shared.message.Message;
import eerbaar.v2.app.GameHelper;
import eerbaar.v2.pattern.client.internal.teach.TeachingCommand;

public class CollisionMsgReader implements IMessageReader {

    private final TeachingCommand cmd;

    public CollisionMsgReader(TeachingCommand cmd) {
        this.cmd = cmd;
    }

    @Override
    public boolean read(Message msg) {

        if (!(msg instanceof CollisionMsg)) {
            return false;
        }
        CollisionMsg collisionMsg = (CollisionMsg) msg;

        IGame game = GameHelper.getGame();
        if (game == null) {
            return false;
        }
        IPersona pet = game.getPersona("pet");

        IMemory memory = pet.getHead().getBrain().getMemory();

        String target = collisionMsg.getTarget();
        String category = null;
        if (target.startsWith("bone") || "house".equals(target)) {
            category = "collision";
        } else if ("dog".equals(target)) {
            category = "collision3";
        } else {
            category = "collision2";
        }
        memory.addEvent(category, target);

        if (this.cmd.isTeaching()) {
            this.cmd.getBody().stopTeaching();
        }

        return false;
    }

}
